package outputstream;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectOutputstream_ {
    //把类FileOutputStream01写入D:\\xuexi\\FileOutputStream01.dat
    //注：FileOutputStream01已实现Serializable序列化接口所以能序列化
    @Test
    public void objectOutputStream01() {
        ObjectOutputStream objectOutputStream = null;
      //  Dog dog = new Dog("大黄", 3);
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\xuexi\\FileOutputStream01.dat"));
            objectOutputStream.writeBoolean(true);
            objectOutputStream.writeByte(3);
            objectOutputStream.writeBytes("A");
            objectOutputStream.writeChar(12);
            objectOutputStream.writeChars("A");
            objectOutputStream.writeUTF("AAHPH");
            objectOutputStream.writeObject(new Dog("大黄", 3));
            System.out.println("完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    class Dog implements Serializable {
        String name;
        int age;

        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
}
